# Copyright 2022 Flant JSC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

{!{- $enableWorkflowOnTestRepos := false -}!}
{!{- $workflowName := "Daily e2e tests" }!}
name: '{!{ $workflowName }!}'
on:
  schedule:
  - cron: '0 1 * * 1-5'
  workflow_dispatch:

env:
  WERF_DRY_RUN: "false"
{!{ tmpl.Exec "werf_envs" | strings.Indent 2 }!}

# Always run a single job at a time.
# Note: Concurrency is currently in beta and subject to change.
# https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#concurrency
concurrency:
  group: e2e-daily

jobs:
  skip_tests_repos:
    name: Skip tests repos
    runs-on: ubuntu-latest
    if: ${{ {!{ $enableWorkflowOnTestRepos }!} || github.repository == 'deckhouse/deckhouse' }}
    steps:
    - name: Do nothing
      run: echo "Empty action to fulfil Github requirements."


# Note: git_info is needed for werf.yaml
{!{- $gitInfoJobCtx := coll.Merge . (dict "dependJobs" (slice "skip_tests_repos")) -}!}
{!{ tmpl.Exec "git_info_job" $gitInfoJobCtx | strings.Indent 2 }!}

{!{- $dependsJobsForAlert := slice "skip_tests_repos" "git_info" -}!}

{!{/* Jobs for each CRI and Kubernetes version */}!}
{!{- $criName := "Containerd" -}!}
{!{- $kubernetesVersion := "1.25" -}!}
{!{- $providerNames := slice "AWS" "Azure" "GCP" "Yandex.Cloud" "OpenStack" "vSphere" "Static" -}!}
{!{- if $enableWorkflowOnTestRepos -}!}
{!{-   $providerNames = slice "AWS" "OpenStack" "Azure" -}!}
{!{- end -}!}
{!{- range $providerName := $providerNames -}!}
{!{-   $provider := $providerName | replaceAll "." "-" | toLower -}!}
{!{-   $kubernetesVersionSlug := $kubernetesVersion | replaceAll "." "_" | toLower -}!}
{!{-   $cri := $criName | toLower -}!}
{!{-   $criEnv := $cri | toUpper -}!}
{!{-   $layout := (tmpl.Exec "e2e_get_layout" (dict "provider" $provider) | strings.TrimSpace ) -}!}
{!{-   $providerForJobId := $providerName | replaceAll "." "_" | toLower -}!}
{!{-   $jobID := printf "run_%s_%s_%s" $providerForJobId $cri $kubernetesVersionSlug -}!}
{!{-   $dependsJobsForAlert = $dependsJobsForAlert | coll.Append $jobID -}!}
{!{-   $jobName := printf "%s, %s, Kubernetes %s" $providerName $criName $kubernetesVersion -}!}
{!{-   $jobCtx := (dict "provider" $provider "cri" $cri "criName" $criName "criEnv" $criEnv "layout" $layout) }!}
{!{-   $jobCtx = coll.Merge $jobCtx (dict "kubernetesVersion" $kubernetesVersion "kubernetesVersionSlug" $kubernetesVersionSlug) }!}
{!{-   $jobCtx = coll.Merge $jobCtx (dict "providerName" $providerName "workflowName" $workflowName "jobName" $jobName "jobID" $jobID) }!}
{!{   tmpl.Exec "e2e_run_job_template" $jobCtx | strings.Indent 2 }!}
{!{- end }!}

  send_alert_about_workflow_problem:
    name: Send alert about workflow problem
    runs-on: ubuntu-latest
    needs: {!{ $dependsJobsForAlert | data.ToJSON }!}
    if: ${{ failure() }}
    steps:
{!{- $labels := dict "trigger" "DailyE2EWorkflowFailed" -}!}
{!{- $annotations := dict "summary" "Daily e2e tests workflow failed" -}!}
{!{- $annotations = coll.Merge $annotations (dict "description" "Check Daily e2e workflow log for more information or see another alerts in this group.") -}!}

{!{- tmpl.Exec "e2e_send_alert_template" (slice (dict "labels" $labels "annotations" $annotations )) | strings.Indent 4 -}!}
